﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Repository.BasicSet;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Domain.ViewModels.BasicSet.RuleOfCode;

namespace WaterRevenueMS.Core.Service.BasicSet.Impl
{
    [Export(typeof(IRuleOfCodeService))]
    public class RuleOfCodeService : CoreServiceBase, IRuleOfCodeService
    {
        [Import]
        public IRuleOfCodeRepository RuleOfCodeRepository { get; set; }

        public IQueryable<RuleOfCode> RuleOfCodes
        {
            get { return RuleOfCodeRepository.Entities; }
        }

        public OperationResult Insert(RuleOfCodeModel model)
        {
            var entity = new RuleOfCode
            {
                Bian_MGZ=model.Bian_MGZ,
                Bian_MDX=model.Bian_MDX,
                DefaultValue=model.DefaultValue,
                CreateDT=DateTime.Now,
                ReviseDT=DateTime.Now,
                OrderNo=model.OrderNo,
                Yong_HBHCD=model.Yong_HBHCD,
                Qu_YDMCD=model.Qu_YDMCD,
                Biao_CDMCD=model.Biao_CDMCD,
                Zi_ZBHCD=model.Zi_ZBHCD,
                Zi_ZBHZL=model.Zi_ZBHZL,
                Shui_BHCD=model.Shui_BHCD,
                Bian_MGZDY=model.Bian_MGZDY
            };
            RuleOfCodeRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public OperationResult Update(RuleOfCodeModel model)
        {
            var entity = RuleOfCodeRepository.Entities.FirstOrDefault(t => t.Id == model.Id);
            if (entity != null)
            {
                entity.Bian_MGZ=model.Bian_MGZ;
                entity.Bian_MDX=model.Bian_MDX;
                entity.DefaultValue=model.DefaultValue;
                entity.ReviseDT=DateTime.Now;
                entity.OrderNo=model.OrderNo;
                entity.Yong_HBHCD=model.Yong_HBHCD;
                entity.Qu_YDMCD=model.Qu_YDMCD;
                entity.Biao_CDMCD=model.Biao_CDMCD;
                entity.Zi_ZBHCD=model.Zi_ZBHCD;
                entity.Zi_ZBHZL=model.Zi_ZBHZL;
                entity.Shui_BHCD=model.Shui_BHCD;
                entity.Bian_MGZDY = model.Bian_MGZDY;
                RuleOfCodeRepository.Update(entity);
            }
            return new OperationResult(OperationResultType.Success, "更新成功");
        }

        public OperationResult Delete(int Id)
        {
            var entity = RuleOfCodeRepository.Entities.FirstOrDefault(t => t.Id == Id);
            if (entity != null)
            {
                RuleOfCodeRepository.Delete(entity);
            }
            return new OperationResult(OperationResultType.Success, "删除成功");
        }
    }
}
